// pages/schools/profession.js
import evn from "../../utils/http/evn"
import { getProfession } from '../../utils/http/api'
Page({

  /**
   * 页面的初始数据
   */
 data: { 
  imgBaseUrl:  evn.dev.imgUrl,
  picUrl: evn.dev.baseUrl + '/',
  professionId: null,
  schoolId: null,
  userInfo: null,      // 只保留一次
  profession: null,    // 专业信息
  schools: null,       // 学校信息
  schoolPics: [],      // 学校图片
  placeIdMap: {
    1: '韩国',
    2: '西班牙',
    3: '新加坡',
    4: '中外合办',
    5: '港澳专区'
  },
  blurTop: 431, // 顶部大图下边框与模糊区域上边框间隔15px（大图高416px+15px）
  capsuleTop: 0, // 胶囊按钮顶部
  eduPlanName:''
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const professionId = options.id || 0;
    // 获取胶囊按钮信息
    const menuButton = wx.getMenuButtonBoundingClientRect();
    const blurTop = menuButton.bottom + 23;
    this.setData({
      professionId: professionId,
      schoolId: options.schoolId,
      userInfo: wx.getStorageSync('userInfo'),
      blurTop: blurTop,
      capsuleTop: menuButton.top // 记录胶囊按钮顶部
    });
    wx.setNavigationBarTitle({ title: '' }); // 隐藏原生标题栏标题
    // 查询专业详细信息
    getProfession({ id: professionId, schoolId: options.schoolId }).then(res => {
      console.log('图片完整地址', this.data.url + (res.schools && res.schools.pics ? res.schools.pics : ''));
      // 兼容接口异常或无数据情况
      let schoolPics = [];
      if (res.schools && res.schools.pics) {
        schoolPics = res.schools.pics;
        if (typeof schoolPics === 'string') {
          schoolPics = schoolPics.split(',');
        }
      }
      // 处理 professionClasses 分组，每行2个
      let programRows = [];
      if (Array.isArray(res.professionClasses)) {
        for (let i = 0; i < res.professionClasses.length; i += 2) {
          programRows.push(res.professionClasses.slice(i, i + 2));
        }
      }
      // 处理授课语言模糊区
      let languageBlurList = [];
      if (res.profession && res.profession.languages) {
        // 支持字符串和数组
        let langs = res.profession.languages;
        if (typeof langs === 'string') {
          langs = langs.split(',').map(s => s.trim());
        }
        langs.forEach(lang => {
          if (lang === '1') {
            languageBlurList.push({
              icon: this.data.imgBaseUrl + '/zhongwen.png',
              text: '中文'
            });
          } else if (lang === '2') {
            languageBlurList.push({
              icon: this.data.imgBaseUrl + '/xiwen.png',
              text: '西班牙语'
            });
          } else if (lang === '3') {
            languageBlurList.push({
              icon: this.data.imgBaseUrl + '/hanwen.png',
              text: '韩语'
            });
          } else if (lang === '4') {
            languageBlurList.push({
              icon: this.data.imgBaseUrl + '/yingwenwen.png',
              text: '英语'
            });
          }else if (lang === '5') {
            languageBlurList.push({
              icon: this.data.imgBaseUrl + '/yingwenwen.png',
              text: '俄语'
            });
          }
        });
      }
      // 处理学位 eduplanName 文本映射
      const eduPlanMap = {
        1: '高升本',
        2: '本升硕',
        3: '专升硕',
        4: '硕升博',
        5: '专升本',
      };
      let degreeText = '';
      if (res.schools && res.schools.eduPlanId) {
        degreeText = eduPlanMap[res.schools.eduPlanId];
      }
      res.profession.eduPlanAdvantage = this.formatContent(res.profession.eduPlanAdvantage)
      this.setData({
        profession: res.profession || null,
        schools: res.schools || null,
        schoolPics: schoolPics,
        programRows: programRows,
        languageBlurList: languageBlurList,
        degreeText: degreeText
      });
    });
  },

  formatContent(html) {
    //给h1替换为p标签，style中的字体大小为28rpx ，颜色为0000000,font-weight为500
    //给h2替换 style中字体为26rpx。颜色为000000,font-weight为500
    //p标签行高伟1.8，颜色为#666D80，自题大小为26rpx
    if(html==null || html==''){
      return ''
    }
    //设置行高1.8
    let newHtml = html.replace(/<p.*?>(.*?)<\/p>/g, '<p style="line-height:1.8;color:#666D80;font-size:26rpx;margin-bottom:32rpx;">$1</p>')
    //设置h1样式
     newHtml = newHtml.replace(/<h1.*?>(.*?)<\/h1>/g, '<p style="font-size:28rpx;color:#000000;font-weight:500;margin-bottom:32rpx;">$1</p>')
     //设置h2样式
    newHtml = newHtml.replace(/<h2.*?>(.*?)<\/h2>/g, '<p style="font-size:26rpx;color:#000000;font-weight:500;margin-bottom:32rpx;">$1</p>')
    return newHtml
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * intro 截断过滤器
   */
  introLimit(str) {
    if (!str) return '';
    let s = str.slice(0, 140);
    if (s.length >= 140) s += '...';
    return s;
  },

  /**
   * 返回按钮点击
   */
  onBackTap() {
    wx.navigateBack({ delta: 1 });
  },
})